import urlFormat from '../common/urlFormat';
import initServerInstance from '../server/initServerInstance';

/**
 * 套餐/余额 aviliableMaxNode used 至为0
 * Job 添加 targetUrl 字段
 */
if (require.main === module) {
	(async () => {
		const server = await initServerInstance({
			loggerConfig: {
				name: 'upgrade',
				alwaysCreate: true,
			},
		});
		const {
			app: { model },
		} = server;
		const { Balance, Job } = model;

		for await (const balance of Balance.find()) {
			balance.balance.aviliableMaxNode.used = 0;
			balance.balance.aviliableMaxNode.charges.forEach(c => {
				c.used = 0;
			});
			await balance.save();
		}

		for await (const job of Job.find()) {
			job.targetUrl = urlFormat(job.target);
			job.pingSettings.forEach(x => {
				x.target = job.targetUrl;
			});
			await job.save();
		}

		process.exit();
	})();
}
